package com.mingqijia.gassafety.console.controller;

import com.gap.basic.base.CommonResponse;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.authority.api.resp.config.DictionaryItemRespDTO;
import com.mingqijia.gassafety.authority.dal.helper.ConfigureHolder;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.DtuDataStatisticsRequest;
import com.mingqijia.gassafety.webserver.request.JvInfoRequest;
import com.mingqijia.gassafety.webserver.response.AreaResponse;
import com.mingqijia.gassafety.webserver.response.CompanyResponse;
import com.mingqijia.gassafety.webserver.response.DtuDataStatisticsResponse;
import com.mingqijia.gassafety.webserver.response.JvInfoResponse;
import com.mingqijia.gassafety.webserver.service.JvInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@Api(tags = "企业接入")
@RequestMapping("/business/v1/jvInfo")
public class JvInfoController {

    @Autowired
    JvInfoService jvInfoService;

    @Autowired
    ConfigureHolder holder;


    @ApiOperation("查询企业列表")
    @PostMapping("/JvList")
    public CommonResponse<PageUtil<JvInfoResponse>> getJvList(@RequestBody JvInfoRequest req){
        String spId = ApplicationUtils.getWorkingSpId();
        if (!"0".equals(spId)){
            throw new BizException("无权限查看日志");
        }
        return CommonResponse.success(jvInfoService.getList(req));
    }

    @ApiOperation("新增企业")
    @PostMapping("/add")
    public CommonResponse<String> add(@Valid @RequestBody JvInfoRequest req) {
        String spId = ApplicationUtils.getWorkingSpId();
        if (!"0".equals(spId)){
            throw new BizException("无权限查看日志");
        }
        try {
            jvInfoService.add(req);
            return CommonResponse.success("创建成功");
        } catch (BizException e) {
            return CommonResponse.failure(e.getMessage());
        }
    }

    @ApiOperation("编辑企业")
    @PostMapping("/edit")
    public CommonResponse<String> edit(@Valid @RequestBody JvInfoRequest req){
        jvInfoService.edit(req);
        return CommonResponse.success("编辑成功");
    }

    @ApiOperation("删除企业")
    @GetMapping("/delete")
    public CommonResponse<String> delete(@RequestParam @ApiParam("id") Integer id){
        jvInfoService.deleteById(id);
        return CommonResponse.success("删除成功");
    }

    @ApiOperation("VCC的企业列表")
    @GetMapping("/companyList")
    public CommonResponse<List<CompanyResponse>> getCompanyList(){
        return CommonResponse.success(jvInfoService.getCompanyList());
    }

    @ApiOperation("公司所属区域列表")
    @GetMapping("/areaList")
    public CommonResponse<List<AreaResponse>> getAreaList(){
        List<DictionaryItemRespDTO> areaList = holder.getDictionaryByKey(Constants.DICTIONARY_JV_AREA);

        List<AreaResponse> resultList = areaList.stream().map(item -> {
            AreaResponse areaResponse = new AreaResponse();
            areaResponse.setAreaCode(item.getValue());
            areaResponse.setAreaName(item.getName());
            return areaResponse;
        }).collect(Collectors.toList());

        return CommonResponse.success(resultList);
    }

    @ApiOperation("报警器数据统计")
    @PostMapping("/dtuData")
    public CommonResponse<List<DtuDataStatisticsResponse>> getDtuList (@RequestBody DtuDataStatisticsRequest request){
        String spId = ApplicationUtils.getWorkingSpId();
        if (!"0".equals(spId)){
            throw new BizException("无权限查看日志");
        }
        return CommonResponse.success(jvInfoService.getDtuList(request));
    }

    @ApiOperation("报警器数据统计更新时间")
    @PostMapping("/dtuDataTime")
    public CommonResponse<String> getUpdateTime (){
        String spId = ApplicationUtils.getWorkingSpId();
        if (!"0".equals(spId)){
            throw new BizException("无权限查看日志");
        }
        return CommonResponse.success(jvInfoService.getUpdateTime());
    }

}
